<template>
  <el-card>
    <template #header>商品列表</template>

    <div>
      <!-- 1.0 表格渲染 -->
      <el-table :data="tableData" style="width: 100%" size="mini">
        <!-- 展开行 -->
        <el-table-column type="expand">
          <template v-slot="{ row }">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="商品ID">
                <span>{{ row.id }}</span>
              </el-form-item>
              <el-form-item label="商品名称">
                <span>{{ row.name }}</span>
              </el-form-item>
              <el-form-item label="商品分类">
                <span>{{ row.category }}</span>
              </el-form-item>
              <el-form-item label="商品名称">
                <span>{{ row.name }}</span>
              </el-form-item>
              <el-form-item label="商品价格">
                <span>{{ row.price }}</span>
              </el-form-item>
              <el-form-item label="创建时间">
                <span>{{ row.ctime }}</span>
              </el-form-item>
              <el-form-item label="商品评价">
                <span>{{ row.rating }}</span>
              </el-form-item>
              <el-form-item label="商品销量">
                <span>{{ row.sellCount }}</span>
              </el-form-item>
              <el-form-item label="商品描述">
                <span>{{ row.goodsDesc }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="name" width="240px">
        </el-table-column>
        <el-table-column label="商品分类" prop="category" width="120px">
        </el-table-column>
        <el-table-column label="商品价格" prop="price"> </el-table-column>
        <el-table-column label="商品图片">
          <template v-slot="{ row }">
            <img
              v-if="row.imgUrl"
              class="goods-img"
              :src="baseUrl + row.imgUrl"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column label="商品描述" prop="goodsDesc" width="240px">
        </el-table-column>
        <el-table-column label="操作" width="150px">
          <template v-slot="{ row }">
            <!-- @click="$router.push('/goods/edit?id=' + row.id)" -->
            <!-- $router.push({ path: '/goods/edit', query: { id: row.id } }) -->
            <!-- @click="$router.push(`/goods/edit/${row.id}`)" -->
            <!-- $router.push({ name: 'goodsEdit', params: { id: row.id } }) -->
            <el-button
              type="primary"
              size="mini"
              @click="$router.push(`/goods/edit/${row.id}`)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" @click="delGoods(row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 2.0 分页处理 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </el-card>
</template>

<script>
import { goodsList, delGoods } from '@/api/goods.js'

export default {
  data() {
    return {
      currentPage: 1,
      pageSize: 5,
      total: 0,
      tableData: [
        {
          id: '12987122',
          name: '好滋好味鸡蛋仔',
          category: '江浙小吃、小吃零食',
          desc: '荷兰优质淡奶，奶香浓而不腻',
          address: '上海市普陀区真北路',
          shop: '王小虎夫妻店',
          shopId: '10333'
        }
      ],
      baseUrl: 'http://127.0.0.1:5000/upload/imgs/goods_img/'
    }
  },
  created() {
    this.getGoods()
  },
  methods: {
    async getGoods() {
      const { data, total } = await goodsList({
        currentPage: this.currentPage,
        pageSize: this.pageSize
      })
      console.log(data)
      this.tableData = data
      this.total = total
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getGoods()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getGoods()
    },
    // 删除商品
    delGoods(id) {
      this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 删除成功的回调
          const { code } = await delGoods({ id })
          // console.log(data)
          if (code === 0) {
            this.getGoods()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    }
  }
}
</script>

<style lang="less" scoped>
.goods-img {
  width: 50px;
  height: 50px;
}

.el-form-item {
  width: 40%;
}
</style>
